﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entities;
using Utility;
using BUS;

public partial class A_LetsDeal_deal_chayhang : System.Web.UI.Page
{
    public int[] sogiay = new int[10];
    public int[] stt_sp = new int[10];
    public int sttt = 1;
    public int div_id = 1;
    public int total = 0;
    public int sosp = 0;
    public int id_hethan = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_data();
        }
    }

    public void load_data()
    {
        pager.PageSize = 10;
        Criteria c = new Criteria();
        String xuly = Request.QueryString.ToString();
        c.add(Condition.AND, Tbl_SanPham_Columns.TinhTrang, Condition.LIKE, "Cháy Hàng");
        //Tìm theo tên từ textbox or querystring
        if (txt_timkiem.Text != "")
        {
            c.add(Condition.AND, Tbl_SanPham_Columns.TenSP, Condition.LIKE,
                txt_timkiem.Text);
        }
        else if (xuly.Contains("tensanpham") == true)
        {
            c.add(Condition.AND, Tbl_SanPham_Columns.TenSP, Condition.LIKE,
                Request.QueryString["tensanpham"].ToString());
        }

        Tbl_SanPham_Collection spCollection =
           Tbl_SanPham_BUS.Get(c.Criter, pager.CurrentIndex, pager.PageSize,
           Tbl_SanPham_Columns.STT.ToString(), OrderDirection.ASC.ToString(), out total);

        Repeater1.DataSource = spCollection;
        Repeater1.DataBind();
        pager.ItemCount = total;

        if (total == 0)
        {
            if (xuly.Contains("tensanpham") == true)
            {
                Literal1.Text = "Kết quả tìm kiếm: Không có sản phẩm cháy hàng nào chứa từ khóa &rsquo;" + Request.QueryString["tensanpham"].ToString() + "&rsquo;";
                pager.Visible = false;
            }
            else if (txt_timkiem.Text != "")
            {
                Literal1.Text = "Kết quả tìm kiếm:  Không có sản phẩm cháy hàng nào chứa từ khóa &rsquo;" + txt_timkiem.Text + "&rsquo;";
                pager.Visible = false;
            }
            else if (xuly.Contains("tinhtrang") == true)
            {
                Literal1.Text = "<font size='+2' color='Red'>Hiện tại chưa có sản phẩm nào bán 'Cháy Hàng'. Ế quá @@!</font>";
                pager.Visible = false;
            }
        }
        if (total < 10)
        {
            sosp = total;
        }
        else
        {
            sosp = 10;
        }
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
                 e.Item.ItemType == ListItemType.Item)
        {
            Tbl_SanPham_Entities sanpham = (Tbl_SanPham_Entities)e.Item.DataItem;

            Literal stt = (Literal)e.Item.FindControl("stt");
            stt.Text = sttt.ToString();
            sttt = sttt + 1;

            HiddenField hd_stt = (HiddenField)e.Item.FindControl("hd_stt");
            hd_stt.Value = sanpham.STT.ToString();

            HyperLink hyp_ten_sp = (HyperLink)e.Item.FindControl("hyp_ten_sp");
            hyp_ten_sp.Text = sanpham.TenSP;
            hyp_ten_sp.NavigateUrl = "~/A_LetsDeal/thongtinsanpham.aspx?id=" + hd_stt.Value;

            Literal lit_giaban = (Literal)e.Item.FindControl("lit_giaban");
            lit_giaban.Text = sanpham.GiaBan.ToString() + "đ";

            Literal lit_giagoc = (Literal)e.Item.FindControl("lit_giagoc");
            lit_giagoc.Text = sanpham.GiaGoc.ToString() + "đ";

            int sodh = Tbl_CTDH_BUS.CountByID(sanpham.STT);
            Literal lit_vccon = (Literal)e.Item.FindControl("lit_vccon");
            lit_vccon.Text = (sanpham.SLBan - sodh).ToString();

            Literal lit_songuoimua = (Literal)e.Item.FindControl("lit_songuoimua");
            lit_songuoimua.Text = sodh.ToString();

            Literal lit_ngayhethan = (Literal)e.Item.FindControl("lit_ngayhethan");
            lit_ngayhethan.Text = sanpham.NgayKetThucDuKien;

            Literal lit_ttsp = (Literal)e.Item.FindControl("lit_ttsp");
            lit_ttsp.Text = sanpham.ThongTinCoBan;

            Tbl_CTDH_Collection ctdh = Tbl_CTDH_BUS.GetByIDSP(sanpham.STT);
            for (int z = 0; z < ctdh.Count; z++)
            {
                Tbl_DonHang_Entities donhang = Tbl_DonHang_BUS.GetByID(ctdh[z].DonHang);
                Tbl_KhachHang_Entities khachhang = Tbl_KhachHang_BUS.GetByID(donhang.NguoiDat);
                ListBox txtdsnguoidat = (ListBox)e.Item.FindControl("txtdsnguoidat");
                if (z == 0)
                    txtdsnguoidat.Items.Add("Danh sách đặt hàng:");
                txtdsnguoidat.Items.Add(txtdsnguoidat.Text + (z + 1).ToString() + "." + khachhang.TaiKhoan + " (" + ctdh[z].SoLuong.ToString() + "vc)");

            }

            HyperLink bt_chitiet = (HyperLink)e.Item.FindControl("bt_chitiet");
            bt_chitiet.NavigateUrl = "~/A_LetsDeal/thongtinsanpham.aspx?id=" + hd_stt.Value.ToString();

            Image imgsp = (Image)e.Item.FindControl("imgsp");
            Image Image1 = (Image)e.Item.FindControl("Image1");
            Tbl_ImageSP_Collection hinh = Tbl_ImageSP_BUS.demhinh(sanpham.STT);
            if (hinh == null)
            {
                imgsp.ImageUrl = "~/Photos/Thumbs/noimages.jpg";
                Image1.Visible = true;
                Image1.ImageUrl = "~/Photos/discount.png";
            }
            else
            {
                imgsp.ImageUrl = "~/Photos/Thumbs/" + hinh[0].ImagePath;
                Image1.ImageUrl = "~/Photos/discount.png";
            }

            DateTime now = DateTime.Now;
            DateTime enddate = DateTime.Parse(sanpham.NgayKetThucDuKien);
            TimeSpan t = enddate - now;
            sogiay[div_id] = int.Parse((Math.Floor(t.TotalSeconds)).ToString());
            stt_sp[div_id] = sanpham.STT;

            for (int a = 1; a <= div_id - 1; a++)
            {
                for (int b = 2; b <= div_id; b++)
                {
                    if (sogiay[b] < sogiay[a])
                    {
                        id_hethan = stt_sp[b];
                    }
                    else
                    {
                        id_hethan = stt_sp[a];
                    }
                }
            }
            div_id = div_id + 1;

            //Repeater Repeater2 = (Repeater)e.Item.FindControl("Repeater2");
            //Criteria c = new Criteria();

            //c.add(Condition.AND, Tbl_DonHang_Columns.STT_sanpham, Condition.EQUAL,
            //      sanpham.STT);
            //Tbl_DonHang_Collection dhCollection =
            //    Tbl_DonHang_BUS.Get(c.Criter, 0, 0,
            //    Tbl_DonHang_Columns.STT.ToString(), OrderDirection.DESC.ToString(), out total);

            //Repeater2.DataSource = dhCollection;
            //Repeater2.DataBind();
        }
    }

    protected void bt_timkiem_Click(object sender, EventArgs e)
    {
        if (txt_timkiem.Text != "")
        {
            Response.Redirect("~/A_LetsDeal/deal_chayhang.aspx?tensanpham=" + txt_timkiem.Text);
        }
    }
}
